package com.qkbb.admin.kuibu.qkbb.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.adapter.FriendListAdapter;
import com.qkbb.admin.kuibu.qkbb.JavaBean.FriendList;
import com.qkbb.admin.kuibu.qkbb.funcation.Friends;
import com.qkbb.admin.kuibu.qkbb.funcation.HttpURLConnHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.SDCardHelper;
import com.qkbb.admin.kuibu.qkbb.url.Url;
import com.qkbb.admin.kuibu.qkbb.view.TitleBarView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class FindFriend extends Activity {
    private EditText editText;
    private ListView listView;
    private String user_token;
    private String nickname;
    private int pager=1;
    private String url;
    private List<Friends> list;
    private Handler handler;
    private FriendListAdapter adapter;
    private URL url1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_friend);
        list=new ArrayList<Friends>();
        TitleBarView titleBarView= (TitleBarView) findViewById(R.id.find_friend_titlebar);
        titleBarView.setCenterTexiView("搜索好友");
        titleBarView.settextColor(Color.WHITE);
        titleBarView.setLeftButton(R.mipmap.fanhui_04);
        titleBarView.setLeftButtonOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        user_token= SDCardHelper.loadFileFromSdCard("user_token",getApplicationContext());
        editText= (EditText) findViewById(R.id.find_friend_edittext);
        listView= (ListView) findViewById(R.id.find_friend_listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Friends friends=list.get(position);
                String userid=friends.getUserid();
                String nickname=friends.getNickname();
                String photo=friends.getPhoto();
                Intent intent=new Intent(FindFriend.this,FriendIformation.class);
                intent.putExtra("userid",userid);
                intent.putExtra("nickname",nickname);
                intent.putExtra("photo",photo);
                startActivity(intent);
            }
        });
        adapter=new FriendListAdapter(list,getApplication());
        handler =new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what==1){
                    adapter.notifyDataSetChanged();
                    listView.setAdapter(adapter);
                }
            }
        };
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (count>0){
                        nickname=s.toString();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                if (list.size()>0){
                                    list.clear();
                                }

//                                url= Url.GETSEARCHFRIEND+user_token+"&nickname="+nickname.toString()+"&page="+pager;
                                StringBuffer buffer=new StringBuffer(Url.GETSEARCHFRIEND);
                                try {
                                    buffer.append(user_token).append("&nickname=").append(URLEncoder.encode(nickname,"utf-8")).append("&page=").append(pager);
                                } catch (UnsupportedEncodingException e) {
                                    e.printStackTrace();
                                }
                                try {
                                    url1=new URL(buffer.toString());
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                Log.e("buffer",url1.toString());
                                byte b[]=HttpURLConnHelper.loadByteFromURL(url1.toString(),getApplication());
                                String str=new String(b);
                                Log.e("str",str);
                                try {
                                    JSONObject jsonObject=new JSONObject(str);
                                    JSONObject jsonObject1=jsonObject.getJSONObject("data");
                                    JSONArray jsonArray=jsonObject1.getJSONArray("FriendData");
                                    for(int i=0;i<jsonArray.length();i++){
                                    Friends friends=new Friends();
                                        JSONObject jsonObject2=jsonArray.getJSONObject(i);
                                        friends.setNickname(jsonObject2.getString("nickname"));
                                        friends.setPhoto(jsonObject2.getString("photo"));
                                        friends.setUserid(jsonObject2.getString("userid"));
                                        list.add(friends);
                                    }
                                    Message message=new Message();
                                    message.what=1;
                                    handler.sendMessage(message);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();


                    }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
